package com.hckj.jianzhijia.networks;

import android.os.AsyncTask;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.hckj.jianzhijia.model.Province;
import com.hckj.jianzhijia.utils.ExceptionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵奇隆 on 2015-11-28.
 */
public class GetProvinceNetWork extends AsyncTask<Void,Void,ArrayList<Province>> {


    private NetWorkInterface netWorkInterface;

    @Override
    protected ArrayList<Province> doInBackground(Void... params) {

        AVQuery avObjectAVQuery = AVQuery.getQuery(Province.TABLE_NAME);
        ArrayList<Province> provinces = new ArrayList<Province>();
        try {
            List list = avObjectAVQuery.find();
            for(int i=0,j=list.size();i<j;i++){
                provinces.add(new Province((AVObject)(list.get(i))));
            }
            return provinces;
        } catch (AVException e) {
            return null;
        }
    }

    public GetProvinceNetWork(NetWorkInterface netWorkInterface) {
        this.netWorkInterface = netWorkInterface;
    }

    @Override
    protected void onPostExecute(ArrayList<Province> provinces) {
        netWorkInterface.afterGetProvinces(provinces);
        super.onPostExecute(provinces);
    }

    public interface NetWorkInterface{
        public void afterGetProvinces(ArrayList<Province> provinces);
    }

}
